package 网络编程.tcp;

import 网络编程.Log;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TCPService {
    public static final int port = 9999;
    private static HashMap<String, String> map = new HashMap<>();
    static {
        map.put("apple","苹果");
        map.put("book","书");
        map.put("bicycle","自行车");
        Log.println("字典初始化完成");
    }

    private static class Task implements Runnable{
        private static Socket socket;
        public Task(Socket socket){
            this.socket = socket;
        }
        @Override
        public void run() {
            try {
                Log.println("准备获取请求方法信息");
                Log.println("请求方IP：" + socket.getInetAddress());//获取客户端的IP)
                Log.println("请求方端口：" + socket.getPort());//获取客户端的端口
                Log.println("请求方IP + port ：" + socket.getRemoteSocketAddress());//获取客户端的IP+port

                //3,获取客户端的输入流
                InputStream inputStream = socket.getInputStream();
                Log.println("请求方的输入流：" + inputStream);
                Scanner sc = new Scanner(inputStream);//将得到的输入流封装到Scanner，输入流都用Scanner就可以接受

                //5，响应请求
                OutputStream outputStream = socket.getOutputStream();
                Log.println("请求方的输出流 ：" + outputStream);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
                PrintWriter writer = new PrintWriter(outputStreamWriter);
                while (true) {
                    if (!sc.hasNextLine()) {
                        break;
                    }
                    String request = sc.nextLine();//获取到数据
                    String engLish = request;
                    Log.println("请求内容  ：" + request);

                    //4，执行业务
                    String chiWord = translate(engLish);
                    String response = chiWord;

                    //5
                    writer.printf("%s\r\n", response);
                    writer.flush();
                    Log.println("开始响应请求");
                    Log.println("响应内容：" + response);
                }
                //6，释放socket资源
                socket.close();
                Log.println("释放请求方socket");
            }catch (Exception e){
                throw  new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) throws  Exception{

        //1.创建TCP套接字
        ServerSocket serverSocket = new ServerSocket(port);
        //2，获取客户端的套接字,没有请求时会阻塞

        ThreadPoolExecutor pool = new ThreadPoolExecutor(3,3,10, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(), new ThreadPoolExecutor.AbortPolicy());
        while (true){
            Log.println("等待连接建立");
            Socket socket = serverSocket.accept();
            Log.println("客户段连接建立，将新的连接提交到线程池");
            //创建新的任务提交到线程池
            pool.submit(new Task(socket));
        }
    }
    private static String translate(String engWord) {
        String chiWord = map.getOrDefault(engWord,"查无此词");
        return chiWord;
    }
}
